package com.hqq.exercise.tree;

import java.util.Scanner;

/**
 * FindMinRoot AVL树最小子树的根节点
 * 题目描述:
 * 满二叉树的深度K,2^k-1个节点,节点值1~2^k-1,给出k和任意3个节点的值,输出包含3个节点，且最小子树的根节点
 * 样例输入:
 * 4 10 15 13
 * 样例输出:
 * 12
 * Created by heqianqian on 2017/8/29.
 */
public class FindMinRoot {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int c = scanner.nextInt();

        int max = Math.max(a, Math.max(b, c));
        int min = Math.min(a, Math.min(b, c));

        int root = getRoot(k);
        while (max < root || min > root) {
            if (max < root) {//在左子树中查找
                k--;
                root -= getRoot(k);
            } else if (min > root) {//在右子树中查找
                k--;
                root += getRoot(k);
            }
        }
        System.out.println(root);
    }

    /**
     * 返回深度为K的AVL树的根节点
     */
    public static int getRoot(int k) {
        int count = 1;
        for (int i = 0; i < k; i++) {
            count *= 2;
        }
        return count;
    }
}
